<template>
  <div class="page-container">
    <el-card>
      <div slot="header">
        <span>支付管理</span>
      </div>

      <div class="toolbar">
        <el-button type="primary" icon="el-icon-plus" @click="handleAdd">创建支付</el-button>
      </div>

      <el-table :data="tableData" border stripe v-loading="loading">
        <el-table-column prop="paymentNo" label="支付单号" width="200"></el-table-column>
        <el-table-column prop="orderId" label="订单ID" width="100"></el-table-column>
        <el-table-column prop="amount" label="支付金额" width="120"></el-table-column>
        <el-table-column prop="status" label="状态" width="100">
          <template slot-scope="scope">
            <el-tag :type="getStatusType(scope.row.status)">
              {{ scope.row.status }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="创建时间" width="180"></el-table-column>
        <el-table-column label="操作" width="200" fixed="right">
          <template slot-scope="scope">
            <el-button size="mini" @click="handleQuery(scope.row)">查询</el-button>
            <el-button size="mini" type="warning" @click="handleRefund(scope.row)">退款</el-button>
          </template>
        </el-table-column>
      </el-table>

      <div class="pagination">
        <el-pagination
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-size="10"
          layout="total, prev, pager, next"
          :total="total"
        ></el-pagination>
      </div>
    </el-card>

    <el-dialog title="创建支付" :visible.sync="dialogVisible" width="500px">
      <el-form :model="form" ref="form" label-width="100px">
        <el-form-item label="订单ID">
          <el-input v-model.number="form.orderId" type="number"></el-input>
        </el-form-item>
        <el-form-item label="支付金额">
          <el-input v-model.number="form.amount" type="number"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="handleSubmit">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { createPayment, queryPayment, refundPayment } from '@/api/payment';

export default {
  name: 'PaymentIndex',
  data() {
    return {
      loading: false,
      tableData: [],
      currentPage: 1,
      total: 0,
      dialogVisible: false,
      form: {
        orderId: null,
        amount: 0
      }
    };
  },
  mounted() {
    this.loadData();
  },
  methods: {
    loadData() {
      this.loading = true;
      setTimeout(() => {
        this.tableData = [
          {
            paymentNo: 'PAY' + Date.now(),
            orderId: 1,
            amount: 100.00,
            status: 'SUCCESS',
            createTime: new Date().toLocaleString()
          }
        ];
        this.total = 1;
        this.loading = false;
      }, 500);
    },
    handleAdd() {
      this.form = { orderId: null, amount: 0 };
      this.dialogVisible = true;
    },
    handleQuery(row) {
      queryPayment(row.paymentNo).then(res => {
        this.$message.success('查询成功');
        this.$alert(JSON.stringify(res.data, null, 2), '支付详情');
      });
    },
    handleRefund(row) {
      this.$confirm('确定要退款吗?', '提示', {
        type: 'warning'
      }).then(() => {
        refundPayment({
          paymentNo: row.paymentNo,
          amount: row.amount
        }).then(() => {
          this.$message.success('退款成功');
          this.loadData();
        });
      });
    },
    handleSubmit() {
      createPayment(this.form).then(() => {
        this.$message.success('创建成功');
        this.dialogVisible = false;
        this.loadData();
      });
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.loadData();
    },
    getStatusType(status) {
      return status === 'SUCCESS' ? 'success' : 'warning';
    }
  }
};
</script>
